#ifndef XMPPCONNECT_H
#define XMPPCONNECT_H
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <openssl/ssl.h>
#include "ice_rtp.h"
#include "micro_libxml.h"
#include "length_define.h"
#define xmpp_connection_t struct xmpp_connection

#define xmpp_session_t struct xmpp_session
struct xmpp_session
{
	char occupied;
	char sessionid[255];
	char local[255];
	char remote[255];
	char initiator[255];
	struct ice_rtp_connection *rtp_connection;
	xmpp_session_t *next;
};
#define SIPXMPP_CONNECTION_STATUS_t enum SIPXMPP_CONNECTION_STATUS
enum SIPXMPP_CONNECTION_STATUS
{
	SIPXMPP_CONNECTION_STATUS_TRYING = 30,
	SIPXMPP_CONNECTION_STATUS_ERROR,
	SIPXMPP_CONNECTION_STATUS_DISCONNECTED,
	SIPXMPP_CONNECTION_STATUS_LOGINED
};
enum SIPXMPP_SESSION_STATUS
{
	SIPXMPP_SESSION_STATUS_ACCEPTED = 40,
	SIPXMPP_SESSION_STATUS_TERMINATED,
	SIPXMPP_SESSION_STATUS_REQUEST//remote call us.
};
struct xmpp_connection;
typedef int (*session_status_change_callback)(struct xmpp_connection *connection,xmpp_session_t *session,enum SIPXMPP_SESSION_STATUS status);
#define sipxmpp_iq_command_t struct sipxmpp_iq_command
struct sipxmpp_iq_command;
struct xmpp_connection
{
	char stopping;
	int readtimeout;
	int fd;
	char *readbuffer;
	char *sendbuffer;
	sipxmpp_iq_command_t *sipxmpp_iq_commands;
	pthread_mutex_t sipxmpp_iq_commands_mutex;
	pthread_t reading_thread;
	struct sockaddr_in serv_addr;
	char sip_username[SIP_USERNAME_LEN];
	char username[128];
	char password[128];
	char domain[128];
	SSL *ssl;
	SSL_CTX *context;
	char jid[128];
	unsigned int serial;
	xmpp_session_t *sessions;
	enum SIPXMPP_SESSION_STATUS status;
	session_status_change_callback session_status_change;
	xmpp_connection_t *next;
};
struct sipxmpp_remote_call_struct
{
	xmpp_connection_t *connection;
	xmpp_session_t *session;
};


int sipxmpp_init();
int sipxmpp_destroy();
int sipxmpp_ping(xmpp_connection_t *connection);
int sipxmpp_create_xmpp_connection(char *server,int port,char *username,char *password,char *sip_username,xmpp_connection_t **connection);
int sipxmpp_destroy_xmpp_connection(xmpp_connection_t *connection);
int start_sipxmpp_login(xmpp_connection_t *connection);
//int sipxmpp_get_jingleinfo(xmpp_connection_t *connection);
xmpp_session_t *sipxmpp_make_call(xmpp_connection_t *connection,const char *to,char *sessionid);
int sipxmpp_terminate_call(xmpp_connection_t *connection,xmpp_session_t *session,const char* reason);
int sip_xmpp_send_accept(xmpp_connection_t *connection,xmpp_session_t *session);
int sipxmpp_presence(xmpp_connection_t *connection);
int sipxmpp_find_connection_by_sip_username(char *sip_username,xmpp_connection_t **xmpp_connection);
#endif
